package com.cqupt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cqupt.service.LLDService;
import com.cqupt.utils.APIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desciption 低级自动发现规则LLD
 * @Date 2021/4/22 16:14
 */
public class LLDServiceImpl implements LLDService {

    /**
     * 根据模板id找到其下的所有监控项原型
     * @param templateidList
     * @return
     */
    public List<Map<String, Object>> getItemPrototypeByTemp(String[] templateidList){
        String token = APIUtils.getToken();
        // 输出过滤
        List<String> itemList = new ArrayList<>();
        itemList.add("itemid");
        itemList.add("key_");
        itemList.add("name");
        itemList.add("discover");// 发现启用 0 还是不启用 1

        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("auth",token);
            put("jsonrpc", "2.0");
            put("method", "itemprototype.get");
            put("id", 1);
            put("params",new HashMap<String,Object>(){{
                put("output",itemList);
//                // 过滤
//                put("filter",new HashMap<String,Object>(){{
//                        put("name",new String[]{
//                                "Interface {#IFNAME}: Bits received",
//                                "Interface {#IFNAME}: Bits sent",
//                                "Interface {#IFNAME}({#IFALIAS}): Bits received",
//                                "Interface {#IFNAME}({#IFALIAS}): Bits sent",
//                                "{#DEVNAME}: Disk read rate",
//                                "{#DEVNAME}: Disk write rate",
//                        });
//                }});
                put("templateids",templateidList);
            }});
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        List<Map<String,Object>> resultList = (List<Map<String,Object>>)responseMap.get("result");
        return resultList;
    }

    /**
     * 根据监控项原型id,更新监控项原型dicovery状态
     * @param itemIds
     * @param itemidNeed
     * @return
     */
    public Map<String,Object> updateItemPrototype(String[] itemIds,String[] itemidNeed){
        String token = APIUtils.getToken();

        boolean flag;
        // 开启需要的监控项原型，剩下的则关闭
        List<Map<String,Object>> itemIdList = new ArrayList<>();
        for (int i = 0; i < itemIds.length; i++) {
            flag = true;
            Map<String,Object> itemId = new HashMap<String,Object>();
            itemId.put("itemid",itemIds[i]);
            for(int j = 0;j < itemidNeed.length; j++){
                if(itemIds[i].equals(itemidNeed[j])){
                    // 需要的监控项原型为 0。不需要的置为1.
                    itemId.put("discover","0");// 0启用，1禁用
                    itemId.put("status",0);// 初始默认启用
                    flag = false;
                    break;
                }
            }
            if(flag){
                itemId.put("discover","1");
            }
            itemIdList.add(itemId);
        }

        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("auth",token);
            put("jsonrpc", "2.0");
            put("method", "itemprototype.update");
            put("id", 1);
            put("params",itemIdList);
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        Map<String,Object> resultList = (Map<String,Object>)responseMap.get("result");
        return resultList;
    }

    /**
     *
     * @param templateidList
     * @return
     */
    @Override
    public List<Map<String, Object>> setDelayNetItemPrototypeByTemp(String[] templateidList) {
        String token = APIUtils.getToken();
        List<String> itemList = new ArrayList<>();
        itemList.add("itemid");
        itemList.add("key_");
        itemList.add("name");
        itemList.add("discover");// 发现启用 0 还是不启用 1
        String[] namelist = new String[]{"*Bits received*","*Bits sent*"};
        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("auth",token);
            put("jsonrpc", "2.0");
            put("method", "itemprototype.get");
            put("id", 1);
            put("params",new HashMap<String,Object>(){{
                put("output",itemList);
//                // 过滤
                put("search", new HashMap<String, Object>(){{
                    put("key_", "net.if");
                    put("name",namelist);
                }});
                put("searchWildcardsEnabled",true);
                put("searchByAny",true);
                put("templateids",new String[]{
                        "10001","10081","10229"
                });
            }});
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        List<Map<String,Object>> resultList = (List<Map<String,Object>>)responseMap.get("result");
        return resultList;
    }

    /**
     *
     * @param prototypeNetItemids
     */
    @Override
    public void updateNetItemPrototype(String[] prototypeNetItemids) {
        String token = APIUtils.getToken();

        List<Map<String,Object>> itemIdList = new ArrayList<>();
        for (int k = 0; k < prototypeNetItemids.length; k++) {
            Map<String,Object> itemId = new HashMap<String,Object>();
            itemId.put("itemid",prototypeNetItemids[k]);
            for(int m = 0;m < prototypeNetItemids.length; m++){
                itemId.put("delay","3m");// 延迟时间设为3分钟
            }
            itemIdList.add(itemId);
        }

        Map<String, Object> paramsMaps = new HashMap<String, Object>(){{
            put("auth",token);
            put("jsonrpc", "2.0");
            put("method", "itemprototype.update");
            put("id", 1);
            put("params",itemIdList);
        }};
        Map<String, Object> responseMaps = APIUtils.sendRequest(JSON.toJSONString(paramsMaps, SerializerFeature.WriteMapNullValue));
    }
}
